﻿using System;
using System.Windows;
using System.Windows.Input;
using RSSFeedReader.Commands;
using RSSFeedReader.Data.Models;
using System.Diagnostics;

namespace RSSFeedReader.ViewModels
{
    /// <summary>
    /// ViewModel to view and edit the properties of a feed.
    /// </summary>
    public class ChannelPropertiesViewModel : ViewModelBase
    {
        #region Fields

        ChannelViewModel _channelViewModel;

        DelegateCommand<string> _okCommand;
        DelegateCommand _goToHomepageCommand;
        DelegateCommand _copyLinkCommand;

        #endregion

        #region Constructor

        /// <summary>
        /// Initialize a new instance of the <see cref="RSSFeedReader.ViewModels.ChannelPropertiesViewModel"/> class.
        /// </summary>
        public ChannelPropertiesViewModel() { }

        /// <summary>
        /// Initialize a new instance of the <see cref="RSSFeedReader.ViewModels.ChannelPropertiesViewModel"/> class.
        /// </summary>
        /// <param name="channelViewModel">The <see cref="RSSFeedReader.ViewModels.ChannelViewModel"/>
        /// instance to use as the data provider.</param>
        public ChannelPropertiesViewModel(ChannelViewModel channelViewModel)
        {
            if (channelViewModel == null)
                throw new ArgumentNullException("folderViewModel");

            _channelViewModel = channelViewModel;
        }

        #endregion

        #region Commands

        #region OKCommand
        /// <summary>
        /// OKCommand to save the changes made.
        /// </summary>
        public ICommand OKCommand
        {
            get
            {
                if (_okCommand == null)
                    _okCommand = new DelegateCommand<string>(DoOKCommand);

                return _okCommand;
            }
        }

        /// <summary>
        /// Executes the OKCommand.
        /// </summary>
        /// <param name="title">The new title of the channel.</param>
        void DoOKCommand(string title)
        {
            try
            {
                _channelViewModel.Title = title;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("An error has occured: {0}", ex.Message));
            }
            finally
            {
                OnRequestClose();
            }
        }
        #endregion

        #region GoToHomepageCommand
        /// <summary>
        /// GoToHomepageCommand to open website homepage
        /// in default web browser.
        /// </summary>
        public ICommand GoToHomepageCommand
        {
            get
            {
                if (_goToHomepageCommand == null)
                    _goToHomepageCommand = new DelegateCommand(DoGoToHomepageCommand);

                return _goToHomepageCommand;
            }
        }

        /// <summary>
        /// Executes the GoToHomepageCommand.
        /// </summary>
        void DoGoToHomepageCommand()
        {
            System.Diagnostics.Process.Start(Link);
        }
        #endregion

        #region CopyLinkCommand
        /// <summary>
        /// Copied the shortcut to the feed homepage.
        /// </summary>
        public ICommand CopyLinkCommand
        {
            get
            {
                if (_copyLinkCommand == null)
                    _copyLinkCommand = new DelegateCommand(DoCopyLinkCommand);

                return _copyLinkCommand;
            }
        }

        /// <summary>
        /// Executes the CopyLinkCommand.
        /// </summary>
        void DoCopyLinkCommand()
        {
            Clipboard.SetText(Link);
        }
        #endregion

        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the title of the channel.
        /// </summary>
        public string Title
        {
            get { return _channelViewModel.Title; }
        }

        /// <summary>
        /// Gets the Url of the channel.
        /// </summary>
        public string Url
        {
            get { return _channelViewModel.Url; }
        }

        /// <summary>
        /// The Image associated with the feed.
        /// </summary>
        public string ImageUrl
        {
            get { return _channelViewModel.ImageUrl; }
        }

        /// <summary>
        /// Gets the link to the channel.
        /// </summary>
        public string Link
        {
            get { return _channelViewModel.Link; }
        }

        /// <summary>
        /// Gets the summary of the channel.
        /// </summary>
        public string Description
        {
            get { return _channelViewModel.Description; }
        }
        #endregion
    }
}
